/**
 * Chess is a game of chess. Maybe this should by in the class GraphicChessboard as the console
 * version of this game is not used anymore.
 * 
 * @author Brian Boadi, Antoine Domenger
 * @version 1.0
 */

package lp.chess;

import javax.swing.JFrame;

import lp.chess.ui.GUIHelper;
import lp.chess.ui.GraphicChessboard;
import lp.chess.ui.MainForm;

public class Chess {
	//- ATTRIBUTE(S) --------------------------------------------------------------------------
	
	/**
	 * The chessboard where this game takes place.
	 */
	private static Chessboard m_cb;
	
	/**
	 * True if the white player is playing, false if it is the black one.
	 */
	private static boolean m_isWhitePlaying;
	
	/**
	 * The current turn.
	 */
	private static int m_turn;
	
	/**
	 * The graphic instance of this game.
	 */
	private static GraphicChessboard m_gc;
	
	/**
	 * The main form used to display this game.
	 */
	private static MainForm m_mf;
	
	/**
	 * The white player.
	 */
	private static WhitePlayer m_wp;
	
	/**
	 * The black player.
	 */
	private static BlackPlayer m_bp;
	
	/**
	 * True if the white player is in check.
	 */
	private static boolean m_isWhiteInCheck;
	
	/**
	 * True if the black player is in check.
	 */
	private static boolean m_isBlackInCheck;
	
	//-----------------------------------------------------------------------------------------
	
	//- CONSTRUCTOR(S) ------------------------------------------------------------------------
	
	/**
	 * Default constructor.
	 * Initializes a new game of chess.
	 */
	public Chess() {
		m_gc = new GraphicChessboard();
		m_isWhitePlaying = true;
		m_isWhiteInCheck = false;
		m_isBlackInCheck = false;
		
		m_mf = new MainForm();
		GUIHelper.showOnFrame(m_mf,"Chess");
		
		m_cb = new Chessboard();
		m_wp = new WhitePlayer();
		m_bp = new BlackPlayer();
		
		m_wp.placePieces(m_cb);
		m_bp.placePieces(m_cb);
		
		setTurn(1);
		
		m_gc.addListeners();
		m_gc.refresh(m_cb);
	}
	
	//-----------------------------------------------------------------------------------------
	
	//- GETTER(S) AND SETTER(S) ---------------------------------------------------------------
	
	public static void setWhitePlayer(WhitePlayer wp) {
		m_wp = wp;
	}
	
	public static void setBlackPlayer(BlackPlayer bp) {
		m_bp = bp;
	}
	
	public static boolean isWhiteInCheck() {
		return m_isWhiteInCheck;
	}
	
	public static boolean isBlackInCheck() {
		return m_isBlackInCheck;
	}
	
	public static void setWhiteInCheck(boolean isWhiteInCheck) {
		m_isWhiteInCheck = isWhiteInCheck;
	}
	
	public static void setBlackInCheck(boolean isBlackInCheck) {
		m_isBlackInCheck = isBlackInCheck;
	}
	
	public static WhitePlayer getWhitePlayer() {
		return m_wp;
	}
	
	public static BlackPlayer getBlackPlayer() {
		return m_bp;
	}
	
	public static void setChessboard(Chessboard cb) {
		m_cb = cb;
	}
	
	public static MainForm getMainForm() {
		return m_mf;
	}
	
	public static GraphicChessboard getGraphicChessboard() {
		return m_gc;
	}
	
	public static Chessboard getChessboard() {
		return m_cb;
	}
	
	public static int getTurn() {
		return m_turn;
	}
	
	public static void setTurn(int turn) {
		m_turn = turn;
		m_mf.updateTurn(m_turn);
	}
	
	public static void setWhiteIsPlaying(boolean whiteIsPlaying) {
		m_isWhitePlaying = whiteIsPlaying;
	}
	
	public static boolean isWhitePlaying() {
		return m_isWhitePlaying;
	}
	
	//-----------------------------------------------------------------------------------------
	
	public static void main(String [] args) {
		Chess game = new Chess();
		
		/*Chessboard cb = new Chessboard();
		WhitePlayer wp = new WhitePlayer();
		BlackPlayer bp = new BlackPlayer();
		
		wp.placePieces(cb);
		bp.placePieces(cb);
		
		cb.display();*/
		
		/*Chessboard cb = new Chessboard();
		WhitePlayer wp = new WhitePlayer();
		BlackPlayer bp = new BlackPlayer();
		
		wp.placePieces(cb);
		bp.placePieces(cb);
		
		cb.display();
		
		King k = new King(true);
		cb.removePiece(5, 1);
		cb.place(k);
		
		cb.display();
		
		if(k.validMove(cb, 5, 2)) System.out.println("Valid move !\n");
		else System.out.println("ERR\n");
		cb.removePiece(6,2);
		if(k.validMove(cb, 6, 2))
		{
			System.out.println("Valid move !\n");
			
			if(cb.move(k, 6, 2)) System.out.println("Moved !\n");
			else System.out.println("ERR\n");
		}
		else System.out.println("ERR\n");
		if(k.validMove(cb, 5, 4)) System.out.println("Valid move !\n");
		else System.out.println("ERR\n");
		
		cb.display();
		
		Knight n = new Knight(true);
		cb.removePiece(5, 1);
		cb.place(n);
		
		cb.display();
		
		if(n.validMove(cb, 6, 3))
		{
			System.out.println("Valid move !\n");
			
			if(cb.move(n, 6, 3)) System.out.println("Moved !\n");
			else System.out.println("ERR\n");
		}
		else System.out.println("ERR\n");
		
		cb.display();*/
	}
}
